package com.demon.framework.common.controller;

import com.demon.framework.common.model.entity.ResultVO;
import com.demon.framework.common.retcode.BaseRetCode;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * web层通用数据处理
 *
 * @author Demon-HY
 */
public class BaseController {

    /**
     * 通用的 操作成功
     */
    public static ResultVO<Void> SUCCESS() {
        return ResultVO.SUCCESS();
    }

    /**
     * 通用的 操作成功时的 返回对象
     */
    public static <T> ResultVO<T> SUCCESS(T result) {
        return ResultVO.SUCCESS(result);
    }

    /**
     * 通用的错误提示方法
     *
     * @param retCode 错误码
     */
    public static ResultVO<Void> ERROR(BaseRetCode.RetCode retCode) {
        return ResultVO.ERROR(retCode);
    }

    /**
     * 通用的错误提示方法
     *
     * @param msg 错误信息
     */
    public static ResultVO ERROR(String msg) {
        return ResultVO.ERROR(msg);
    }

    /**
     * 通用的错误提示方法
     *
     * @param msg     错误信息
     * @param retCode 编码
     */
    public static ResultVO<Void> ERROR(String msg, String retCode) {
        return ResultVO.ERROR(msg, retCode);
    }

    /**
     * 获取request
     */
    public HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return requestAttributes.getRequest();
    }

    /**
     * 获取response
     */
    public HttpServletResponse getResponse() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return requestAttributes.getResponse();
    }

    /**
     * 获取session
     */
    public HttpSession getSession() {
        return getRequest().getSession();
    }
}
